package swings.frames;

import swings.labels.ParamLabel;
import swings.others.CommonJTextField;
import utils.CommonUtils;
import utils.ImageUtils;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @version V1.0
 * @Title: 编辑自增序列frame
 * @Package
 * @Description:
 * @author: runoob
 * @date:
 */
public class OperateSeqFrame extends JFrame {

    private Container contentPane = this.getContentPane();
    private JPanel jrbPanel = new JPanel();
    private JPanel valuePanel = new JPanel();
    private JLabel jrbLabel = new JLabel("Add timestamp :", JLabel.RIGHT);
    private JLabel valueLabel = new JLabel("Value :", JLabel.RIGHT);
    private JLabel resultLabel = new JLabel("Result :", JLabel.RIGHT);
    private JLabel resultDataLabel = new JLabel("", JLabel.LEFT);
    private CommonJTextField valueField = new CommonJTextField(50);
    private JRadioButton yesJrb = new JRadioButton("YES", true);
    private JRadioButton noJrb = new JRadioButton("NO");
    private ButtonGroup buttonGroup = new ButtonGroup();
    private JButton saveBtn = new JButton("Save");
    private Font font = CommonUtils.getFont();
    private Color bgColor = CommonUtils.bgColor;
    private Color inputColor = new Color(191,191,191);
    private ParamLabel paramLabel;

    /**
     * 构造方法
     */
    public OperateSeqFrame(ParamLabel paramLabel){
        this.paramLabel = paramLabel;
        init();
        initBtn();
        this.setTitle("OperateSeq");
        this.setIconImage(ImageUtils.logo);
        this.setLocation((CommonUtils.screenWidth-400)/2, (CommonUtils.screenHeight-220)/2);
        this.setSize(400,220);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    public void init(){
        contentPane.setLayout(null);
        jrbPanel.setBounds(0,5,390,40);
        jrbPanel.setLayout(null);
        jrbPanel.setBackground(bgColor);
        jrbPanel.setForeground(Color.WHITE);
        valuePanel.setLayout(null);
        valuePanel.setBounds(0,45,390,40);
        valueLabel.setFont(font);
        valueLabel.setForeground(Color.WHITE);
        valueLabel.setBounds(45,0,120,40);
        resultLabel.setFont(font);
        resultLabel.setForeground(Color.WHITE);
        resultLabel.setBounds(45,85,120,40);
        resultDataLabel.setFont(font);
        resultDataLabel.setForeground(Color.WHITE);
        resultDataLabel.setBounds(195,85,160,40);
        valuePanel.setBackground(bgColor);
        valueField.setFont(font);
        valueField.setBackground(inputColor);
        valueField.setForeground(Color.WHITE);
        valueField.setBounds(200,7,150,25);
//        valueField.addFocusListener(new valueFieldLis());
        valueField.getDocument().addDocumentListener(new valueFieldLis());
        jrbLabel.setFont(font);
        jrbLabel.setForeground(Color.WHITE);
        jrbLabel.setBounds(15,0,150,40);
        yesJrb.setBounds(195,0,80,40);
        noJrb.setBounds(295,0,80,40);
        yesJrb.setBackground(bgColor);
        noJrb.setBackground(bgColor);
        yesJrb.setForeground(Color.WHITE);
        noJrb.setForeground(Color.WHITE);
        yesJrb.setFont(font);
        noJrb.setFont(font);
        yesJrb.addActionListener(new jrbLis());
        noJrb.addActionListener(new jrbLis());
        saveBtn.setBounds(160,140,70,30);
        saveBtn.setFont(font);
        jrbPanel.add(jrbLabel);
        jrbPanel.add(yesJrb);
        jrbPanel.add(noJrb);
        buttonGroup.add(yesJrb);
        buttonGroup.add(noJrb);
        valuePanel.add(valueLabel);
        valuePanel.add(valueField);
        contentPane.add(valuePanel);
        contentPane.add(jrbPanel);
        contentPane.add(resultLabel);
        contentPane.add(resultDataLabel);
        contentPane.add(saveBtn);
        contentPane.setBackground(bgColor);
        resultDataLabel.setText(System.currentTimeMillis() + "");
    }

    public void initBtn(){
        //保存按钮监听器
        saveBtn.addActionListener(e -> {
            String result = valueField.getText();
            if (yesJrb.isSelected()){
                if (result.length()>0){
                    result = "timestamp + " + result;
                }else {
                    result = "timestamp";
                }
            }
            paramLabel.valueField.setText(result);
            dispose();
        });
    }

    /**
     * hold监听器
     */
    private class valueFieldLis implements DocumentListener {

        @Override
        public void insertUpdate(DocumentEvent e) {
            updateResult(e);
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            updateResult(e);
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            updateResult(e);
        }
    }

    /**
     * hold监听器
     */
    private class jrbLis implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            String result = valueField.getText();
            if (yesJrb.isSelected()){
                String timestamp = System.currentTimeMillis() + "";
                result = timestamp + result;
            }
            resultDataLabel.setText(result);
        }
    }

    public void updateResult(DocumentEvent e){
        Document doc = e.getDocument();
        String result = "";
        try {
            result = doc.getText(0, doc.getLength());
        } catch (BadLocationException ex) {
            ex.printStackTrace();
        }
        if (yesJrb.isSelected()){
            String timestamp = System.currentTimeMillis() + "";
            result = timestamp + result;
        }
        resultDataLabel.setText(result);
    }

}
